import utilfuncs from "../../../common/tools/utilfuncs";
var funcs = utilfuncs.UtilFuncs;

import globalConst from "../../../common/globalConst";
var global = globalConst.GlobalConst;

import { LGXEnums } from "../../../common/sysenum";


import lgxcom from '../../../common/common'
import { GlobalConstObj } from "../../../common/globalconstobj";

const offsetgap = 30;

class LineCoordCorrectSvc {
    constructor(dataMng) {
        this.dataManager = dataMng;
    }

    act2Mouse(lineGraph, mosEvnt, mousePt) {
        var position = lineGraph.getPosCopy();
        let poscount = position.posList.length;
        if (lineGraph.visionType != LGXEnums.lineVisionType.cross || poscount < 2) {
            return;
        }

        let lineLenth = position.calLenth();
        if (lineLenth < global.SelectGap) {
            return;
        }

        let testPt = {x:565,y:345};
        let dis = funcs.distancePoint(mousePt,testPt,1);
        if (dis < global.SelectGap){
            console.log("");
            dis += 10; 
        }

        let preFinish = lineGraph.getFinish();
        lineGraph.setFinish(0);//避免判定头尾相交图形时，取到了自己
        let headPt = position.getHead();
        let headConnGraph = this.dataManager.pickGraph4Edge(headPt);
        let tailPt = position.getTail();
        let tailConnGraph = this.dataManager.pickGraph4Edge(tailPt);
        if (tailConnGraph == headConnGraph) {
            tailConnGraph = null;
        }

        if (poscount == 2 && mosEvnt.type == LGXEnums.LGXMouseMode.LMouseUp){
            let kind = funcs.chkHornVertKind(headPt,tailPt);
            if (kind == 1){
                //近似水平
                tailPt.y = headPt.y;
                lineGraph.setPos(position);
            }
            else if (kind == 2){
                //近似垂直
                tailPt.x = headPt.x;
                lineGraph.setPos(position);
            }
        }

        if (tailConnGraph && tailConnGraph.m_graphType == LGXEnums.LGXGraphType.CircleGraph_type){
            console.log("");
        }
        this.startCrossConnLineGraph(headPt, lineGraph, headConnGraph, tailConnGraph);
        lineGraph.setFinish(preFinish);
        return;
    }
    startCrossConnLineGraph(nodePt, lineGraph, curentGraph, anotherEndConnGraph) {
        let position = lineGraph.getPosCopy();
        let ptCount = position.posList.length;
        let headPt = position.getHead();
        let tailPt = position.getTail();

        //把跟当前移动图形相连的点作为起始点对待
        let revertPosDir = false;//反向处理线路坐标点顺序
        let stPoint = headPt;
        let endPoint = tailPt;
        let nextStIdx = 1;
        let next2EndIdx = ptCount - 2;
        let dis1 = funcs.distancePoint(nodePt, headPt, 1);
        let dis2 = funcs.distancePoint(nodePt, tailPt, 1);
        if (dis2 < dis1) {
            nextStIdx = ptCount - 2;
            next2EndIdx = 1;
            stPoint = tailPt;
            endPoint = headPt;
            revertPosDir = true;
        }
        let ptNext2St = position.posList[nextStIdx];
        let ptNext2End = position.posList[next2EndIdx];
        let stDir = this.getLineEndDirRefConnGraph(stPoint, curentGraph, ptNext2St);
        let endDir = this.getLineEndDirRefConnGraph(endPoint, anotherEndConnGraph, ptNext2End);

        //从起点沿起始搜索方向，离开起始图形外框一个固定距离的点
        let seartStPt = this.getValidStPoint(stPoint, stDir, curentGraph);

        //从起始搜索点seartStPt开始，获得正确的搜索方向，因为前面初始化的搜索方向可能会导致搜索路径跟起始图形交叉
        let validDir = this.getValidStDir(seartStPt, stDir, curentGraph, endPoint);

        //找准起始搜索路径后，找真正开始起始搜索点，此点向目标点的射线一定与起始图形不交叉
        let finalValidStPt = this.getFinalStPt(seartStPt, validDir, curentGraph, endPoint);

        //从终点，反起始搜索方向，离开终点图形外框一个固定距离的点
        let seartEndPt = this.getValidStPoint(endPoint, endDir, anotherEndConnGraph);
        let validEndDir = this.getValidStDir(seartEndPt, endDir, anotherEndConnGraph, stPoint);
        let finalValidEndPt = this.getFinalStPt(seartEndPt, validEndDir, anotherEndConnGraph, stPoint);

        let searctPtList = [];
        searctPtList.push(stPoint);
        searctPtList.push(seartStPt);
        searctPtList.push(finalValidStPt);

        //遇到交叉图形时，搜索向量固定转向
        let turnDirDegree = -90;//右转
        let orignalVector = funcs.getVector(stPoint, endPoint);
        let orignalVectorDegree = orignalVector.angleFrmXAxisPositiveDir();
        if ((orignalVectorDegree >= 0 && orignalVectorDegree <= 90)
            || (orignalVectorDegree >= 180 && orignalVectorDegree <= 270)) {
            //第一、第三象限
            turnDirDegree = 90;//左转
        }
        let confilictGraphList = [];
        confilictGraphList.push(curentGraph);
        this.crossLine(finalValidStPt, validDir, seartEndPt, endDir,
            anotherEndConnGraph, confilictGraphList, turnDirDegree,
            searctPtList);
        // searctPtList.push(finalValidEndPt);
        searctPtList.push(seartEndPt);
        searctPtList.push(endPoint);

        searctPtList = funcs.removeCollapsePoint(searctPtList);
        searctPtList = this.correctTo2ReducePoint(searctPtList);//修正减少拐点

        let newPtList = [];
        if (revertPosDir) {
            let count = searctPtList.length;
            for (let i = count - 1; i >= 0; i--) {
                let pt = searctPtList[i];
                newPtList.push(pt);
            }
        }
        else {
            let count = searctPtList.length;
            for (let i = 0; i < count; i++) {
                let pt = searctPtList[i];
                newPtList.push(pt);
            }
        }
        position.clear();
        newPtList.forEach(pt => {
            position.addPoint(pt);
        });
        lineGraph.setPos(position);
        lineGraph.calScope(1);
    }

    //计算线路连接点坐标相对于连接图形的起始走向向量
    getLineEndDirRefConnGraph(point, connGraph, next2Point) {
        let destVector = new lgxcom.LGXVector();
        if (!connGraph) {
            return destVector;
        }

        if (connGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type
            || connGraph.m_graphType == LGXEnums.LGXGraphType.CommBusGraph_type) {
            //如果点连在线路（连接线、母线都是线路）上，则当前正在绘制的连接线在此位置的方向要与被连接线路垂直
            let ptConnLineSecVector = this.calPointConnLineSecVector(point, connGraph);
            let lineEndVct = funcs.getVector(point, next2Point);//连接线当前判定点到邻近点坐标的向量
            let rotLeftVct = ptConnLineSecVector.rotateCopy(90);//point在目标线路上的向量左旋90°的向量
            let rotRightVct = ptConnLineSecVector.rotateCopy(-90);//point在目标线路上的向量左旋90°的向量

            //对比左旋右旋向量到当前线段向量的旋转角，小的就是本函数所要的目标向量
            let rotAngle1 = rotLeftVct.angleByCrossMultiply2Another(lineEndVct);
            let rotAngle2 = rotRightVct.angleByCrossMultiply2Another(lineEndVct);
            destVector = rotLeftVct;
            if (rotAngle2 < rotAngle1) {
                destVector = rotRightVct;
            }
        }
        else {
            let rect = connGraph.getZone();
            let cntPt = rect.getCnt();

            let connGraphAbsorbDir = this.getAbsorbDir(rect,point);
            if (!connGraphAbsorbDir) {
                let vector = funcs.getVector(cntPt, point);
                let degree = vector.angleFrmXAxisPositiveDir();
                if ((degree >= 0 && degree <= 45) || (degree >= 315 && degree <= 360)) {
                    destVector.setValue(1, 0, 0);
                }
                else if (degree >= 45 && degree <= 135) {
                    destVector.setValue(0, 1, 0);
                }
                else if (degree >= 135 && degree <= 225) {
                    destVector.setValue(-1, 0, 0);
                }
                else if (degree >= 225 && degree <= 315) {
                    destVector.setValue(0, -1, 0);
                }
                else {
                    destVector.setValue(1, 0, 0);
                }
            }
            else {
                let rotLeftVct = connGraphAbsorbDir.rotateCopy(90);//point在目标线路上的向量左旋90°的向量
                let cntProj2Left = funcs.getPointProj2Line(cntPt, point, rotLeftVct);//中心點投影到左旋綫路上
                let destPt2ProjLeftPtVector = funcs.getVector(point, cntProj2Left);
                if (!rotLeftVct.equal(destPt2ProjLeftPtVector)) {
                    destVector = rotLeftVct;
                }
                else {
                    let rotRightVct = connGraphAbsorbDir.rotateCopy(-90);//point在目标线路上的向量左旋90°的向量
                    let cntProj2Right = funcs.getPointProj2Line(cntPt, point, rotRightVct);//中心點投影到右旋綫路上
                    let destPt2ProjRightPtVector = funcs.getVector(point, cntProj2Right);
                    if (!rotRightVct.equal(destPt2ProjRightPtVector)) {
                        destVector = rotRightVct;
                    }
                }
            }
        }
        return destVector;
    }

    getValidStPoint(stPoint, stDir, stGraph) {
        if (!stGraph) {
            return stPoint
        }
        let seartStPt = { x: 0, y: 0 };
        seartStPt.x = stPoint.x + stDir.m * offsetgap;
        seartStPt.y = stPoint.y + stDir.n * offsetgap;
        if (!funcs.isLineGraph(stGraph.getType())) {
            let rect = stGraph.getZone();
            if (stDir.m == 1) {
                seartStPt.x = rect.right + offsetgap;
                seartStPt.y = stPoint.y;
            }
            else if (stDir.m == -1) {
                seartStPt.x = rect.left - offsetgap;
                seartStPt.y = stPoint.y;
            }
            else if (stDir.n == 1) {
                seartStPt.x = stPoint.x;
                seartStPt.y = rect.top + offsetgap;
            }
            else if (stDir.n == -1) {
                seartStPt.x = stPoint.x;
                seartStPt.y = rect.bottom - offsetgap;
            }
        }
        return seartStPt;
    }

    //找准起始搜索路径后，找真正开始起始搜索点，此点向目标点的射线一定与起始图形不交叉
    getFinalStPt(stPoint, stDir, stGraph, endPoint) {
        let seartStPt = { x: 0, y: 0 };
        seartStPt.x = stPoint.x;
        seartStPt.y = stPoint.y;
        if (!stGraph) {
            return seartStPt;
        }

        let rect = stGraph.getZone();
        let interRst = funcs.getLineSecInterRectPoint(stPoint, endPoint, rect);
        if (interRst && interRst.flag) {
            if (stDir.m == 1) {
                seartStPt.x = rect.right + offsetgap;
                seartStPt.y = stPoint.y;
            }
            else if (stDir.m == -1) {
                seartStPt.x = rect.left - offsetgap;
                seartStPt.y = stPoint.y;
            }
            else if (stDir.n == 1) {
                seartStPt.x = stPoint.x;
                seartStPt.y = rect.top + offsetgap;
            }
            else if (stDir.n == -1) {
                seartStPt.x = stPoint.x;
                seartStPt.y = rect.bottom - offsetgap;
            }
        }

        return seartStPt;

    }


    crossLine(stPoint, preDir, endPoint, endDir, endConnGraph,
        confilictGraphList, turnDirDegree, searctPtList) {
        //获得起点沿着preDir向量的射线与末端点相应正交射线的正交点
        let endCrossPoint = this.getSt2EndCrossPoint(stPoint, preDir, endPoint, endDir);

        //获得起点沿着preDir正交向量的射线与末端点相应正交射线的正交点
        let endCrossPoint1 = this.getSt2EndCrossPoint(stPoint, preDir.rotateCopy(90), endPoint, endDir);

        //检测当前迭代线段是否与其他图形交叉、冲突
        let straightConflict1 = this.chkConflictOtherGraph(stPoint, endCrossPoint, endConnGraph);//直行方向起点到正交点是否与周边交叉
        let straightConflict2 = this.chkConflictOtherGraph(endCrossPoint, endPoint, endConnGraph);//直行方向正焦点到末端点是否与周边交叉
        let crossConflictInfo1 = this.chkConflictOtherGraph(stPoint, endCrossPoint1, endConnGraph);//正交方向起点到正交点是否与周边交叉
        let crossConflictInfo2 = this.chkConflictOtherGraph(endCrossPoint1, endPoint, endConnGraph);//正交方向正交点到末端点是否与周边交叉

        if (!straightConflict1 && !straightConflict2) {
            //直行方向两段线都无交叉，则结束搜索
            searctPtList.push(endCrossPoint);
        }
        else if (!crossConflictInfo1 && !crossConflictInfo2) {
            //正交方向两段线都无交叉，则结束搜索
            searctPtList.push(endCrossPoint1);
        }
        else {
            //两个方向都有交叉，取到交叉点路径最长方向为下次搜索方向
            let straightLenth = 0
            let crossLenth = 0;

            let straightConflictGraph = null;
            let crossConflictGraph = null;

            //直走方向
            let straightNextStPoint = { x: 0, y: 0 };//从碰撞点回退一个固定距离的点，从该点做转向搜索
            let straightInterPt = null;//直走方向与图形交叉点
            let straightInterFlag = false;//true标识相交在第二段
            if (straightConflict1) {
                straightInterPt = straightConflict1.pt;
                straightLenth = funcs.distancePoint(stPoint, straightInterPt, 1);

                let revertVct = preDir.invertCopy();
                straightNextStPoint.x = straightInterPt.x + revertVct.m * offsetgap;
                straightNextStPoint.y = straightInterPt.y + revertVct.n * offsetgap;
                straightConflictGraph = straightConflict1.graph;
            }
            else if (straightConflict2) {
                straightNextStPoint.x = endCrossPoint.x;
                straightNextStPoint.y = endCrossPoint.y;
                straightInterPt = straightConflict2.pt;
                straightLenth = funcs.distancePoint(stPoint, endCrossPoint, 1);
                straightLenth += funcs.distancePoint(endCrossPoint, straightInterPt, 1);
                straightInterFlag = true;
                straightConflictGraph = straightConflict2.graph;
            }

            //正交方向
            let crossNextStPoint = { x: 0, y: 0 };//从碰撞点回退一个固定距离的点，从该点做转向搜索
            let crossInterPt = null;//正交方向搜索，与其他图形的交叉点
            let crossInterFlag = false;//true标识相交在第二段
            if (crossConflictInfo1) {
                crossInterPt = crossConflictInfo1.pt;
                crossLenth = funcs.distancePoint(stPoint, crossInterPt, 1);

                let revertVct = funcs.getVector(stPoint, crossInterPt).invertCopy();
                crossNextStPoint.x = crossInterPt.x + revertVct.m * offsetgap;
                crossNextStPoint.y = crossInterPt.y + revertVct.n * offsetgap;
                crossConflictGraph = crossConflictInfo1.graph;
            }
            else if (crossConflictInfo2) {
                crossNextStPoint.x = endCrossPoint1.x;
                crossNextStPoint.y = endCrossPoint1.y;

                crossInterPt = crossConflictInfo2.pt;
                crossLenth = funcs.distancePoint(stPoint, endCrossPoint1, 1);
                crossLenth += funcs.distancePoint(endCrossPoint1, crossInterPt, 1);
                crossInterFlag = true;
                crossConflictGraph = crossConflictInfo2.graph;
            }

            let preConflictValidPt = { x: straightNextStPoint.x, y: straightNextStPoint.y };//本次碰撞检测中，有效碰撞点的有效拐点
            let destConfilictGraph = straightConflictGraph;//本次碰撞检测最终采用的交叉图形
            let conflictPoint = { x: straightInterPt.x, y: straightInterPt.y };
            let curentSearchDir = new lgxcom.LGXVector();
            curentSearchDir.setBy2Pt(stPoint, endCrossPoint);
            if (straightInterFlag) {
                curentSearchDir.setBy2Pt(endCrossPoint, straightInterPt);
            }
            else {
                curentSearchDir.clone(preDir);
            }
            let useStraightPt = true;
            if (crossLenth > straightLenth) {
                destConfilictGraph = crossConflictGraph;
                useStraightPt = false;
                conflictPoint = { x: crossInterPt.x, y: crossInterPt.y };
                preConflictValidPt = { x: crossNextStPoint.x, y: crossNextStPoint.y };
                curentSearchDir.setBy2Pt(stPoint, endCrossPoint1);
                if (crossInterFlag) {
                    curentSearchDir.setBy2Pt(endCrossPoint1, crossInterPt);
                }
                else {
                    curentSearchDir.setBy2Pt(stPoint, endCrossPoint1);
                }
            }
            if (confilictGraphList.indexOf(destConfilictGraph) >= 0) {
                //递归失败，直接在递归路径最后一点和目标点之间生成一段直角拐线
                let searchFinalPt = searctPtList[searctPtList.length - 1];
                let crossPt = this.getSt2EndCrossPoint(searchFinalPt, preDir, endPoint, endDir);
                searctPtList.push(crossPt);
                return;
            }
            else {
                confilictGraphList.push(destConfilictGraph);
            }

            //碰撞点回退
            let conflictBackPt = { x: 0, y: 0 };
            let revertVct = curentSearchDir.invertCopy();
            conflictBackPt.x = conflictPoint.x + revertVct.m * offsetgap;
            conflictBackPt.y = conflictPoint.y + revertVct.n * offsetgap;

            //在碰撞点尝试向左转、向右转，避开交叉图形，寻找下个
            // let vct = funcs.getVector(conflictBackPt, endPoint);
            // let turnLeftVct = curentSearchDir.rotateCopy(90);
            // let turnRightVct = curentSearchDir.rotateCopy(-90);

            // //左转时，判断左转向量到目标向量的旋转角或者反过来，取绝对值小者
            // let leftAngle = turnLeftVct.angleByCrossMultiply2Another(vct);
            // let rightAngle = turnRightVct.angleByCrossMultiply2Another(vct);

            // let destVector = new lgxcom.LGXVector();//最终采用的转向向量
            // destVector.clone(turnLeftVct);
            // if (rightAngle < leftAngle) {
            //     destVector.clone(turnRightVct);
            // }

            let destVector = curentSearchDir.rotateCopy(turnDirDegree);
            //根据转向向量确定避开此交叉图形的拐点
            let rect = destConfilictGraph.getZone();
            let nextStPoint = this.getNextCrossPt(conflictBackPt, destVector, rect);
            searctPtList.push(preConflictValidPt);
            searctPtList.push(conflictBackPt);
            searctPtList.push(nextStPoint);

            this.crossLine(nextStPoint, destVector, endPoint, endDir,
                endConnGraph, confilictGraphList, turnDirDegree, searctPtList);
        }

    }

    getNextCrossPt(stPoint, stDir, rect) {
        let nextCrossPt = { x: 0, y: 0 };
        if (stDir.m == -1) {
            nextCrossPt.x = rect.left - offsetgap;
            nextCrossPt.y = stPoint.y;
        }
        else if (stDir.m == 1) {
            nextCrossPt.x = rect.right + offsetgap;
            nextCrossPt.y = stPoint.y;
        }
        else if (stDir.n == 1) {
            nextCrossPt.x = stPoint.x;
            nextCrossPt.y = rect.top + offsetgap;
        }
        else if (stDir.n == -1) {
            nextCrossPt.x = stPoint.x;
            nextCrossPt.y = rect.bottom - offsetgap;
        }
        return nextCrossPt;
    }


    //检测当前迭代线段是否与其他图形交叉、冲突
    chkConflictOtherGraph(stPoint, endCrossPoint, endConnGraph) {
        let conflictGraphInfoList = this.getConflictGraphs(stPoint, endCrossPoint);
        //距离排序
        let sortedInterInfoList = conflictGraphInfoList.sort(function (p1, p2) {
            if ((p1.dis - p2.dis) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );
        let conflictInfo = null;
        if (sortedInterInfoList.length > 0) {
            conflictInfo = sortedInterInfoList[0];
        }
        return conflictInfo;
    }

    getConflictGraphs(stPt, endPt) {
        let conflictGraphInfoList = [];
        let tAlllGraphList = this.dataManager.getAllGraphs();
        let count = tAlllGraphList.length;
        for (let i = 0; i < count; i++) {
            let pGraph = tAlllGraphList[i];
            if (pGraph.getUseful() != 1
                || pGraph.m_graphType == LGXEnums.LGXGraphType.MultiSel_Rect_Type
                || pGraph.m_graphType == LGXEnums.LGXGraphType.FastToolPanel_type
                || pGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type
                || pGraph.m_graphType == LGXEnums.LGXGraphType.CommBusGraph_type
                || pGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
                continue;
            }

            let rect = pGraph.getZone();
            if (funcs.chkPtInRect(stPt,rect) || funcs.chkPtInRect(stPt,rect)){
                continue;
            }
            let rst = funcs.getLineSecInterRectPoint(stPt, endPt, rect);
            if (rst && rst.flag) {
                let t = { graph: pGraph, dis: rst.dis, pt: rst.pt };
                conflictGraphInfoList.push(t);
            }
        }
        return conflictGraphInfoList;
    }

    //沿着起点方向迭代，获得起点沿着stDir向量的射线与末端点可能的正交点
    getSt2EndCrossPoint(stPoint, stDir, endPoint, endDir) {
        let endCrossPoint = { x: 0, y: 0 };
        if (stDir.parallel(endDir)) {
            //如果两个向量平行，则判断是否是否完全重合，如果不能重合则需要从末端方向正交后取与起始射线的交点
            let endNextPoint = { x: 0, y: 0 };
            endNextPoint.x = endPoint.x + endDir.m;
            endNextPoint.y = endPoint.y + endDir.n;
            let dis = funcs.pointToLineDistance(stPoint, endPoint, endNextPoint);
            if (dis < global.ZERO_4E) {
                //完全重合，则直接取末端点为正交点
                endCrossPoint.x = endPoint.x;
                endCrossPoint.y = endPoint.y;
            }
            else {
                //不重合，则末端射线正交
                let crossVct = endDir.rotateCopy(90);
                let interPoint = funcs.getTwoLineInterPos(stPoint, stDir, endPoint, crossVct);
                if (interPoint) {
                    endCrossPoint.x = interPoint.x;
                    endCrossPoint.y = interPoint.y;
                }
                else {
                    endCrossPoint.x = endPoint.x;
                    endCrossPoint.y = endPoint.y;
                }
            }
        }
        else {
            //不平行，则肯定正交
            let interPoint = funcs.getTwoLineInterPos(stPoint, stDir, endPoint, endDir);
            if (interPoint) {
                endCrossPoint.x = interPoint.x;
                endCrossPoint.y = interPoint.y;
            }
            else {
                endCrossPoint.x = endPoint.x;
                endCrossPoint.y = endPoint.y;
            }
        }
        return endCrossPoint;
    }


    //修正减少拐点
    //从起始第二个点开始，取一段线作为初始检测线，再取其后面第三段线作为比较对象
    //（因为检测线的后第一段跟它肯定相交，后面第二段跟它肯定平行，这两段线不能作为比较对象）
    correctTo2ReducePoint(pointList) {
        if (pointList.length < 6) {
            return pointList;
        }
        let retList = [];
        pointList.forEach(pt => {
            retList.push(pt);
        });

        let endFlag = false;
        let stIdx = 1;
        do {
            if ((stIdx + 4) >= retList.length - 2) {
                break;
            }
            let srcLinept1 = retList[stIdx];
            let srcLinept2 = retList[stIdx + 1];

            let dstLinept1 = retList[stIdx + 3];
            let dstLinept2 = retList[stIdx + 4];
            let interPoint = funcs.getTwoLineInterPos_4Point(srcLinept1, srcLinept2, dstLinept1, dstLinept2);
            if (interPoint) {
                //检测第一段线末端点到交点之间的连接线是否与其他图形交叉
                let conflictGraphInfoList1 = this.getConflictGraphs(srcLinept2, interPoint);
                if (conflictGraphInfoList1 && conflictGraphInfoList1.length > 0) {
                    //有交叉，不能合并，继续检测下一个
                    stIdx++;
                    continue;
                }

                //检测第二段线末端点到交点之间的连接线是否与其他图形交叉
                let conflictGraphInfoList2 = this.getConflictGraphs(dstLinept1, interPoint);
                if (conflictGraphInfoList2 && conflictGraphInfoList2.length > 0) {
                    //有交叉，不能合并，继续检测下一个
                    stIdx++;
                    continue;
                }
                //都没有交叉，从第i+1点坐标值改成交点坐标值，i+2,i+3点从队列中删除
                srcLinept2.x = interPoint.x;
                srcLinept2.y = interPoint.y;

                retList.splice(stIdx + 2, 2);
            }
        }
        while (!endFlag);

        return retList;
    }


    chkLineHornvert(linePtList) {
        let haveSplashLineSec = false;
        let prePt = linePtList[0];
        let curentPt = null;
        for (let i = 1; i < linePtList.length; i++) {
            curentPt = linePtList[i];
            let detax = Math.abs(curentPt.x - prePt.x);
            let detay = Math.abs(curentPt.y - prePt.y);
            if (detax > global.ZERO_4E && detay > global.ZERO_4E) {
                haveSplashLineSec = true;
                break;
            }
            prePt = curentPt;
        }
        return !haveSplashLineSec;
    }

    chkIsLineConnGraph(list, lineGraph) {
        let stNodeConnGraph = null;
        let endNodeConnGraph = null;
        if (lineGraph.getTopoRes()) {
            let stNode = lineGraph.getTopoRes().stNode;
            if (stNode && stNode.getRefRes()) {
                stNodeConnGraph = stNode.getRefRes().getRefGraph();
            }

            let endNode = lineGraph.getTopoRes().endNode;
            if (endNode && endNode.getRefRes()) {
                endNodeConnGraph = endNode.getRefRes().getRefGraph();
            }
        }

        if (!stNodeConnGraph && !endNodeConnGraph) {
            return false;
        }

        let haveOtherGraph = false;
        for (let i = 0; i < list.length; i++) {
            let info = list[i];
            if (info.graph != stNodeConnGraph && info.graph != endNodeConnGraph) {
                haveOtherGraph = true;
                break;
            }
        }
        return !haveOtherGraph;
    }

    getValidStDir(stPoint, stDir, stGraph, endPoint) {
        let destVector = new lgxcom.LGXVector();
        destVector.clone(stDir);
        if (!stGraph) {
            return destVector;
        }
        let rect = stGraph.getZone();
        let interRst = funcs.getLineSecInterRectPoint(stPoint, endPoint, rect);
        if (interRst && interRst.flag) {
            let vct = funcs.getVector(stPoint, endPoint);
            let turnLeftVct = stDir.rotateCopy(90);
            let turnRightVct = stDir.rotateCopy(-90);
            let leftAngle = turnLeftVct.angleByCrossMultiply2Another(vct);
            let rightAngle = turnRightVct.angleByCrossMultiply2Another(vct);
            destVector.clone(turnLeftVct);
            if (rightAngle < leftAngle) {
                destVector.clone(turnRightVct);
            }
        }
        return destVector;
    }


    //计算点连在线上的位置，并返回该段线的向量
    calPointConnLineSecVector(point, lineDevGraph) {
        let position = lineDevGraph.getPosCopy();
        let posList = position.posList;
        let ptCount = posList.length;
        if (ptCount < 2) {
            return GlobalConstObj.xAaxisVector;
        }

        let destVector = new lgxcom.LGXVector();
        let prePt = posList[0];
        if (lineDevGraph.chkClose()) {
            posList.push(prePt);
        }
        ptCount = posList.length;
        for (let i = 1; i < ptCount; i++) {
            let curentPt = posList[i];
            if (funcs.chkPointOnLineSec(point, prePt, curentPt)) {
                destVector.setBy2Pt(prePt, curentPt);
                break;
            }
            prePt = curentPt;
        }
        return destVector;
    }

    //返回point在当前图形上的吸附点所在图形段切线向量
    getAbsorbDir(rect,point){
        let posList = rect.convert4Pt2List();
        let destDir =  new lgxcom.LGXVector();
        let minDis = -1;
        let p0 = posList[0];
        posList.push(p0);
        let lenth = posList.length;
        for (var i = 1; i < lenth; i++) {
            let p1 = posList[i];
            let lineSecVector = funcs.getVector(p0,p1);
            let projPt = funcs.getPointProj2Line(point,p0,lineSecVector);//目标点投影到当前线段
            if (!projPt){
                projPt = point;
            }
            if (funcs.chkPointOnLineSec(projPt,p0,p1)){
                let tmpDir = funcs.getVector(p0,p1);
                let dis = funcs.distancePoint(point,projPt,1);
                if (minDis < 0){
                    destDir = tmpDir;
                    minDis = dis;
                }
                else if (minDis > dis){
                    destDir = tmpDir;
                    minDis = dis;
                }
            }
            p0 = p1;
        }
        return destDir;
    }

}

export { LineCoordCorrectSvc }